<!DOCTYPE html>
<html>
<head>
    <div th:include="base-libs :: base-libs" />
    <script th:src="@{/yyzyzx/js/easyui_editModel.js}"></script>
    <title>导入Excel</title>

    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=UTF-8">

    <style>
        .h4-title{
            margin:0
        }
        .excelFont{
            display:inline-block;
            font-size:15px;
            width:75px;
            margin:20px 0px;
            font-weight: bold;
        }
        .file{
            position:absolute;
            top:0; right:80px;
            height:24px;
            filter:alpha(opacity:0);
            opacity: 0;
            width:260px
        }
        #fileName{
            width:200px;
            text-align:center;
            font-size:15px;
            letter-spacing:1px
        }
        .ylBtn{
            float: right;
            margin-top: 6px;
            margin-right: 4px;
        }
    </style>

    <script>
        var yjksArr,bqxxArr;
        $(function(){
            $(this).registerManagerPage();
            initZy();
        })

        function initZy(){
            $.QuickRemote.AjaxJson("queryKsAndBq", function(result){
                yjksArr = result.YJKSXX;
                bqxxArr = result.BQXX;
                initView();
                $("body").css("visibility","visible")
            }, null);
        }

        function initView(){
            //初始化表单
            $('#ylTb').datagrid({
                border:false,
                rownumbers:true,
                singleSelect : true,
                fit:true,
                fitColumns: true,
                columns:[[
                    {field:'XZQHDM', title:'行政区<br/>划代码', width: 50, align: 'center'}
                    ,{field:'YYDJMC', title:'医院等级', width: 90, align: 'center',formatter:function(value){
                        if(!StringUtils.isEmpty(value)){
                            return '<span title="' + value + '">'+ value + '</span>';
                        }
                    }}
                    ,{field:'YLJGMC', title:'医疗机构名称', width: 95, align: 'center',formatter:function(value){
                        if(!StringUtils.isEmpty(value)){
                            return '<span title="' + value + '">'+ value + '</span>';
                        }
                    }}
                    ,{field:'YJKSDM', title:'科室名称', width: 90, align: 'center',
                        formatter:function(value,row){
                            $.each(yjksArr, function(i, item){
                                if(item.KSMC==value){
                                    row.YJKSDM = item.KSDM;
                                    row.YJKSMC = item.KSMC;
                                    return false;
                                }
                                if(item.KSDM==value){
                                    row.YJKSMC = item.KSMC;
                                    value = item.KSMC;
                                    return false;
                                }
                            })
                            return value;
                        },
                        editor:{
                            type: "quickSelectValidate",
                            options:{
                                valueField:'KSDM',
                                textField:'KSMC',
                                ID: 'YJKSDM',
                                data: yjksArr,
                                label: '科室名称',
                                validateProxy:'notNull',
                            }
                        }
                    }
                    ,{field:'BQDM', title:'病区名称', width: 90, align: 'center',
                        formatter:function(value,row){
                            $.each(bqxxArr, function(i, item){
                                if(item.BQMC==value){
                                    row.BQDM = item.BQDM;
                                    row.BQMC = item.BQMC;
                                    return false;
                                }
                                if(item.BQDM==value){
                                    row.BQMC = item.BQMC;
                                    value = item.BQMC;
                                    return false;
                                }
                            })
                            return value;
                        },
                        editor:{
                            type: "quickSelectValidate",
                            options:{
                                valueField:'BQDM',
                                textField:'BQMC',
                                ID: 'BQDM',
                                data: bqxxArr,
                                label: '病区名称',
                                validateProxy:'notNull',
                            }
                        }
                    }
                    ,{field:'SJKFCWS', title:'实际开<br/>放床位数', width: 55, align: 'center',
                        editor:{
                            type: "quickTextValidate",
                            options:{
                                label: '实际开放床位数',
                                length:8,
                                validateProxy:'notNull length num',
                            }
                        }
                    }
                    ,{field:'DQKCS', title:'当前<br/>空床数', width: 55, align: 'center',
                        editor:{
                            type: "quickTextValidate",
                            options:{
                                label: '当前空床数',
                                length:8,
                                validateProxy:'notNull length num',
                            }
                        }
                    },
                    {field : "YYYCWS",title : "已预约<br/>床位数",width:50, align:'center',
                        editor:{
                            type: "quickTextValidate",
                            options:{
                                label: '已预约床位数',
                                length:8,
                                validateProxy:'notNull length num',
                            }
                        }
                    }
                    ,{field:'DQKYYCWS', title:'当前可预<br/>约床位数', width: 55, align: 'center',
                        editor:{
                            type: "quickTextValidate",
                            options:{
                                label: '当前可预约床位数',
                                length:8,
                                validateProxy:'notNull length num',
                            }
                        }
                    }
                    ,{field:'STNYJKCS', title:'3天内预<br/>计空床数', width: 55, align: 'center',
                        editor:{
                            type: "quickTextValidate",
                            options:{
                                label: '3天内预计空床数',
                                length:8,
                                validateProxy:'notNull length num',
                            }
                        }
                    }
                    ,{field:'STNKYYCWS', title:'3天内可预<br/>约床位数', width: 60, align: 'center',
                        editor:{
                            type: "quickTextValidate",
                            options:{
                                label: '3天内可预约床位数',
                                length:8,
                                validateProxy:'notNull length num',
                            }
                        }
                    }
                    ,{field:'YZNYJKCS', title:'1周内预<br/>计空床数', width: 55, align: 'center',
                        editor:{
                            type: "quickTextValidate",
                            options:{
                                label: '1周内预计空床数',
                                length:8,
                                validateProxy:'notNull length num',
                            }
                        }
                    }
                    ,{field:'YZNKYYCWS', title:'1周内可预<br/>约床位数', width: 60, align: 'center',
                        editor:{
                            type: "quickTextValidate",
                            options:{
                                label: '1周内可预约床位数',
                                length:8,
                                validateProxy:'notNull length num',
                            }
                        }
                    }
                    ,{field:'DDCWS', title:'等待<br/>床位数', width: 55, align: 'center',
                        editor:{
                            type: "quickTextValidate",
                            options:{
                                label: '等待床位数',
                                length:8,
                                validateProxy:'notNull length num',
                            }
                        }
                    }
                ]],
                onClickRow : function(rowIndex, row){
                    var editRowNum = $(this).data("editRowNum");
                    if(!StringUtils.isEmpty(editRowNum) && rowIndex != editRowNum){
                        //不做数据验证（避免和添加按钮中的验证重复）
                        $(this).datagrid('endEdit', editRowNum).datagrid('beginEdit', rowIndex)
                            .data("editRowNum",rowIndex);
                    }
                },
                rowStyler: function(index,row){
                    /*if(row.errInfo){
                        return 'background:yellow';
                    }*/
                }
            });

            /** 一级科室和病区信息联动  */
            $("body").on("change",'#YJKSDM',function(){
                var val = $(this).val();
                var result = [];
                $.each(bqxxArr,function(){
                    if(this.KSDM == val){
                        result.push(this);
                    }
                })
                $("#BQDM").createOptions(result,{textField : "BQMC" , valueField : "BQDM"});
            })

            /* 选择文件 */
            $("#btnXz").click(function(){
                $("#fileField").click();
            })

            /* 上传文件  */
            $("#btnSc").click(function(){
                $("#ylTb").datagrid("loadData", []);
                var fileName = $("#fileName").val().trim();
                var regXls = /\.xls$/;
                if(StringUtils.isEmpty(fileName)){
                    $.QuickAlert.alertFail({content: '请选择你需要上传的Excel!'});
                }else if(!regXls.test(fileName)){
                    $.QuickAlert.alertFail({content: '文件类型错误！请选择一个后缀名为 xls 的Excel！'});
                }else{
                    $.QuickRemote.AjaxFiledFormSubmit("excelForm", parseExcelBack, null);
                }
                function parseExcelBack(result){
                    if(result.state){
                        $("#totalRow").html(result.data.length);
                        $("#errorRow").html(result.errorRow);
                        $("#ylTb").datagrid("loadData", result.data).data("editRowNum",'-1'); //清空选中记录
                    }else{
                        $.QuickAlert.alertFail({content: result.info});
                    }
                }
            })

            /* 下载模板 */
            $("#btnXzmb").click(function () {
                $("#downExcelModel").submit();
            })

            /* 下载值域 */
            $("#btnXzzy").click(function () {
                $("#downExcelModelZy").submit();
            })
            
            /** 确认导入 */
            $("#btnQrdr").click(function(){
                $("#ylTb").datagrid('endEdit', $(ylTb).data("editRowNum"));
                var rows = $("#ylTb").datagrid("getRows");
                if(saveValidate()){
                    $.QuickRemote.AjaxJson("/" + contextRoot + "cwyygl/saveBatch", function(result){
                        if(result.state){
                            $.QuickAlert.alertNormal(result.info, {callback: function(){
                                $("#btnQxdr").trigger("click");
                            }});
                        }else{
                            $.QuickAlert.alertFail({content: result.info});
                        }
                    }, {rows: JSON.stringify(rows)});
                }
            })

            /** 取消导入（返回） */
            $("#btnQxdr").click(function(){
                $.QuickUrlUtils.redirectUrl("index.html");
            })
        }

        function saveValidate(){
            var $addTb = $("#ylTb");
            var data = $addTb.datagrid("getRows");
            if(data.length==0) {
                $.QuickAlert.alertFail({content: "请至少添加一条数据!"});
                return false;
            }
            var validate = true;
            $addTb.datagrid('endEdit', $addTb.data("editRowNum")); //保存正在编辑的行的数据
            $.each(data, function(editRowNumber){
                $addTb.data("editRowNum",editRowNumber).datagrid('selectRow', editRowNumber).datagrid('beginEdit', editRowNumber);
                if(!$(".easyuiEditText").QuickValidate()) {
                    validate = false;
                    return false;
                }else{
                    $addTb.datagrid('endEdit', editRowNumber);
                }
            })
            return validate;
        }

        /* 删除 */
        function removeRow(){
            var row = $('#ylTb').datagrid('getSelected');
            if(!row){
                validateLayerAlert("请选择需要删除的数据！");
            }else{
                $.QuickAlert.alertConfirm({
                    content : "确认删除选中的数据？",
                    callback : function() {
                        var index = $("#ylTb").datagrid('getRowIndex',row);
                        $("#ylTb").datagrid('deleteRow',index);
                    }
                })
            }
        }
    </script>
</head>

<body class="easyui-layout" style="visibility: hidden">
<div region="north" style="border: none; overflow: hidden;width:100%">
    <form id="downExcelModel" action="downExcelModel" method="post"></form>
    <form id="downExcelModelZy" action="downExcelModelZy" method="post"></form>
    <form id="excelForm" action="parseExcalData" method="post" enctype="multipart/form-data">
        <h4 class="h4-title">选择上传文件</h4>
        <div  class="form-table-box" style="margin-left:120px">
            <span class="excelFont">Excel文件</span>
            <input type='text' name='fileName' id='fileName' readonly="readonly" onchange="this.title=this.value" />
            <input type="file" name="fileField" class="file" id="fileField" size="28" onchange="document.getElementById('fileName').value=this.value;document.getElementById('fileName').title=this.value" />
            <btnBox style="position: relative;left: 7px;top: -1px;">
                <button type="button" id="btnXz">选择</button>
                <button type="button" id="btnSc">上传</button>
                <button type="button" id="btnXzmb">下载模板</button>
                <button type="button" id="btnXzzy">下载值域</button>
            </btnBox>
            <!--<br/>
            <span id="totalRow">0</span>条记录，其中<span id="errorRow" style="color:red;font-size:16px;padding: 0 5px;">0</span>条记录存在错误-->
        </div>
    </form>
</div>
<div  region="center" style="border: none;width:100%">
    <div class="easyui-layout" style="width:100%">
        <div region="north" style="border: none; overflow: hidden;width:100%">

            <h4 class="h4-title" style="margin-top: 0px;">信息预览
                <button class="ylBtn" type="button" id="btnQxdr">返回</button>
                <button class="ylBtn" type="button" onclick="removeRow()">删除</button>
                <button class="ylBtn" type="button" id="btnQrdr">确认导入</button>
            </h4>
        </div>
        <div  region="center" style="border: none;">
            <table id="ylTb" ></table>
        </div>
    </div>
</div>
</body>


</html>
